/**
 * [OElove] (C)2010-2099 OELOVE.COM Inc. X
 * Email: service@phpcoo.com ,phpcoo@qq.com
 * This is NOT a freeware, use is subject to license terms
 * $ Last update 2016/11/24 by CL $
*/
$(function(){

	//授权登录信息
	$mod = $("#mod").val();
	$openid = $("#openid").val();
	$open_username = $("#open_username").val();
	$open_sex = $("#open_sex").val();
	$open_province = $("#open_province").val();
	$open_city = $("#open_city").val();
	$open_headimg = $("#open_headimg").val();

	//绑定登录
	$(document).on("blur", "#loginname", function(){
		if ($(this).val().length > 0) {
			$("#loginname_tips").hide();
		}
	});
	$(document).on("blur", "#password", function(){
		if ($(this).val().length > 0) {
			$("#password_tips").hide();
		}
	});
	$(document).on("click", "[f='but_loginpost']", function(){
		$loginname = $("#loginname").val();
		$password = $("#password").val();
		if ($loginname.length == 0) {
			$("#loginname_tips").show();
			return false;
		}
		if ($password.length == 0) {
			$("#password_tips").show();
			return false;
		}
		$.ajax({
			type: "POST",
			url: _ROOT_PATH + "index.php?c=passport",
			cache: false,
			data: {
					a:"bindlogin", loginname:$loginname, password:$password, 
					mod:$mod, openid:$openid, open_username:$open_username, open_sex:$open_sex, 
					open_province:$open_province, open_city:$open_city, open_headimg:$open_headimg, 
					r:get_rndnum(8)
			},
			dataType: "json",
			success: function($data) {
				$json = eval($data);
				$response = $json.response;
				$result = $json.result;
				if ($response == "1") {
					$forward = "";
					if ($forward.length == 0) {
						$forward = _ROOT_PATH+"index.php?m=user";
					}
					window.location.href = $forward;					
				}
				else {
					if ($result.length > 0) {
						ToastShow($result);
					}
					else {
						ToastShow("登录失败，请检查帐号密码是否正确");
					}
				}
			},
			error: function() {
				ToastShow("系统繁忙，请稍后再试");
			}
		});

	});



	//创建帐号
	$(document).on("blur", "#mobile", function(){ //检测手机号码
		$_tips = $("#mobile_tips");
		$val_mobile = $(this).val();
		if (!isMobile($val_mobile)) {
			$_tips.html("请填写有效的手机号码");
			$_tips.show();
		}
		else {
			//检测是否可以注册
			$.ajax({
				type: "POST",
				url: _ROOT_PATH + "index.php?c=passport&a=check",
				cache: false,
				data: {check_type:"mobile", name:$val_mobile, r:get_rndnum(8)},
				dataType: "json",
				success: function($data) {
					$json = eval($data);
					$response = $json.response;
					$result = $json.result;
					if ($response == "1") {
						$_tips.html("");
						$_tips.hide();
					}
					else {
						$error = "该手机号已被注册";
						if ($result.length > 0) {
							$error = $result;
						}
						$_tips.html($error);
						$_tips.show();
					}

				},
				error: function() {
					//ToastShow("系统繁忙，请稍候再试");
				}
			});
		}
	});

	//获取手机验证码
	$(document).on("click", "[f='send_mobilecode']", function(){
		$val_mobile = $("#mobile").val();
		$("#mobile_tips").html("");
		$("#mobile_tips").hide();
		if (!isMobile($val_mobile)) {
			$("#mobile_tips").html("请填写有效的手机号码");
			$("#mobile_tips").show();
			return false;
		}
		$(this).html("获取中...");
		$.ajax({
			type: "POST",
			url: _ROOT_PATH + "index.php?c=passport&a=send",
			cache: false,
			data: {send_type:"mobile", name:$val_mobile, r:get_rndnum(8)},
			dataType: "json",
			success: function($data) {
				$json = eval($data);
				$response = $json.response;
				$result = $json.result;
				if ($response == "1") {
					ToastShow("发送成功，请查收手机短信！");
					_countDown("btn_send_mobilecode", "send_mobilecode");
				}
				else {
					if ($result.length > 0) {
						ToastShow($result);
					}
					else {
						ToastShow("发送失败，请检查手机号码是否可用");
					}
					
				}
				$("#btn_send_mobilecode").html("获取验证码");

			},
			error: function() {
				//ToastShow("系统繁忙，请稍后再试");
				$("#btn_send_mobilecode").html("获取验证码");
			}
		});

	});
	
	//检查手机验证码
	$(document).on("blur", "#mobilecode", function(){
		$_tips = $("#mobilecode_tips");
		$_tips.html("");
		$_tips.hide();

		if ($("#mobile").val().length > 0 && $(this).val().length > 0) {
			$.ajax({
				type: "POST",
				url: _ROOT_PATH + "index.php?c=passport&a=check",
				cache: false,
				data: {check_type:"mobilecode", name:$("#mobile").val(), checkcode:$(this).val(),  r:get_rndnum(8)},
				dataType: "json",
				success: function($data) {
					$json = eval($data);
					$response = $json.response;
					$result = $json.result;
					if ($response == "1") {
						$_tips.html("");
						$_tips.hide();
					}
					else {
						$error = "手机验证码错误";
						if ($result.length > 0) {
							$error = $result;
						}
						$_tips.html($error);
						$_tips.show();
					}

				},
				error: function() {
					//ToastShow("系统繁忙，请稍候再试");
				}
			});
		}
	});

	//创建时间
	$(document).on("click", "[f='but_create_account']", function(){
		$("#mobile_tips").html("");
		$("#mobile_tips").hide();
		$("#mobilecode_tips").html("");
		$("#mobilecode_tips").hide();

		$mobile = $("#mobile").val();
		$mobilecode = $("#mobilecode").val();
		if (typeof($mobilecode) == "undefined") {
			$mobilecode = "--";
		}

		if (!isMobile($mobile)) {
			$("#mobile_tips").html("请填写有效的手机号码");
			$("#mobile_tips").show();
			return false;
		}
		if ($mobilecode != "--") {
			if ($mobilecode.length == 0) {
				$("#mobilecode_tips").html("请填写手机验证码");
				$("#mobilecode_tips").show();
				return false;
			}
		}
		
		$("#page_loading_box").show();
		$.ajax({
			type: "POST",
			url: _ROOT_PATH + "index.php?c=passport",
			cache: false,
			data: {
					a:"authlogin", mobile:$mobile, mobilecode:$mobilecode,
					mod:$mod, openid:$openid, open_username:$open_username, open_sex:$open_sex, 
					open_province:$open_province, open_city:$open_city, open_headimg:$open_headimg,
					r:get_rndnum(8)
			},
			dataType: "json",
			success: function($data) {
				$json = eval($data);
				$response = $json.response;
				$result = $json.result;
				$attid = $json.attid;
				if (typeof($attid) == "undefined") {
					$attid = "";
				}
				if ($response == "1") {
					_oeloveFileSynYun($attid); //同步云存储
					$forward = "";
					if ($forward.length == 0) {
						$forward = _ROOT_PATH+"index.php?m=user";
					}
					$("#create_password").html($result);
					$("[f='but_confirm_success']").attr("data-forward", $forward);
					$("#create_success_box").show();
					/*
					setTimeout(function(){
						window.location.href = $forward;
					}, 1000);
					*/

				}
				else {
					$("#page_loading_box").hide();
					if ($result.length > 0) {
						ToastShow($result);
					}
					else {
						ToastShow("创建失败，请检查资料！");
					}
				}

			},
			error: function() {
				$("#page_loading_box").hide();
				ToastShow("系统繁忙，请稍后再试");
			}
		});

	});
	
	//创建帐号成功，确定密码 7.04.20
	$(document).on("click", "[f='but_confirm_success']", function(){
		$forward = $(this).attr("data-forward");
		if ($forward.length == 0) {
			$forward = _ROOT_PATH+"index.php?m=user";
		}
		window.location.href = $forward;
	});

});

//倒计时
var cd_timer = 60; //1分钟
function _countDown(obj_id, attr_f) {
	$obj = $("#"+obj_id);
	if (cd_timer == 0) {
		$obj.html("获取验证码");
		$obj.removeClass("current");
		$obj.attr("f", attr_f);
		cd_timer = 60; 
	}
	else {
		$obj.removeClass("current");
		$obj.addClass("current"); 
		$obj.html(cd_timer+"秒重新发送");
		$obj.attr("f", "");
		cd_timer--; 
		setTimeout(function() { 
			_countDown(obj_id, attr_f)
		},	1000);
	}
} 